<?php

// 活动报名
// POST activity.signUp

namespace app\api\resource\activity;
use think\Db;
use think\Session;

class SignUp
{
    // 方法
    public $_method = ['POST'];
    // 前置
    public $_pre    = ['login','isCyz'];
    // 描述
    public $_description = '活动报名';
    // 参数
    public $_param  = [
        'activity_id' => '活动主键',
        'project_id'  => '可选,项目主键(路演必需)',
    ];
    // 返回
    public $_return = [
        201 => '报名成功',
        400 => '活动不存在|项目不存在|项目未缴纳诚信保证金|不能重复报名该活动',
        422 => '活动主键必需|项目主键必需',
        500 => '服务器异常',
    ];

    public function run(&$request)
    {
        $activityId = $request->post('activity_id/d',0);
        $projectId  = $request->post('project_id/d',0);

        if (0 == $activityId) return [422,'活动主键必需'];
        if (0 == $projectId)  return [422,'项目主键必需'];
        
        $ret = Db::name('activity')->where('id',$activityId)->find();
        if ( is_null($ret) ) return [400,'活动不存在'];
        
        $ret = Db::name('project')->where('id',$projectId)->find();
        if ( is_null($ret) ) return [400,'项目不存在'];
        if ( !$ret['is_cx_pay'] ) return [400,'项目未缴纳诚信保证金'];
        
        $memberId = Session::get('member.id');

        $ret = Db::name('activity_signup')->where('activity_id',$activityId)->where('member_id',$memberId)->find();
        if ( !is_null($ret) ) return [400,'不能重复报名该活动'];

        $memberInfo = Db::name('member_info')->where('member_id',$memberId)->find();

        $data['activity_id'] = $activityId;
        $data['project_id']  = $projectId;
        $data['member_id']   = $memberInfo['member_id'];
        $data['nickname']    = $memberInfo['nickname'];
        $data['headimgurl']  = $memberInfo['headimgurl'];
        $data['create_time'] = time();
        $num = Db::name('activity_signup')->insert($data);

        return ($num > 0) ? [ 201,[] ] : 500;
    }
}